package com.emm.thirdapp;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.content.FileProvider;
import android.view.View;
import android.widget.TextView;

import com.leagsoft.emm.camera.EMMCameraUtil;
import com.leagsoft.emm.camera.callback.PhotoCallBack;
import com.leagsoft.emm.camera.model.PhotoResultBean;

import java.io.File;
import java.util.List;

public class SafeCameraActivity extends Activity implements View.OnClickListener {

    TextView tv_album, tv_camera, tv_delete, tv_Result, tv_emmPhoto, tv_emmVideo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
        tv_camera = (TextView) findViewById(R.id.tv_camera);
        tv_camera.setOnClickListener(this);
        tv_album = (TextView) findViewById(R.id.tv_album);
        tv_album.setOnClickListener(this);
        tv_delete = (TextView) findViewById(R.id.tv_delete);
        tv_delete.setOnClickListener(this);
        tv_Result = (TextView) findViewById(R.id.tv_Result);
        tv_emmPhoto = findViewById(R.id.tv_emmPhoto);
        tv_emmPhoto.setOnClickListener(this);
        tv_emmVideo = findViewById(R.id.tv_emmVideo);
        tv_emmVideo.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        tv_Result.setText("");
        if (id == R.id.tv_camera) {
            //如果EMM后台初始化配置未开启位置权限locationName返回null
            EMMCameraUtil.getPhoto(this, 9, EMMCameraUtil.FROM_CAMERA, "地理位置", new PhotoCallBack() {

                @Override
                public void callBackListen(List<PhotoResultBean> list) {
                    setResult(list);
                }
            });
        } else if (id == R.id.tv_album) {
            EMMCameraUtil.getPhoto(this, 9, EMMCameraUtil.FROM_ALBUM, new PhotoCallBack() {
                @Override
                public void callBackListen(List<PhotoResultBean> list) {
                    setResult(list);
                }
            });
        } else if (id == R.id.tv_delete) {
            EMMCameraUtil.deleteTempPhoto(this);
        } else if (id == R.id.tv_emmPhoto) {
            useEmmCamera(EMMCameraUtil.IS_PHOTO);
        } else if (id == R.id.tv_emmVideo) {
            useEmmCamera(EMMCameraUtil.IS_VIDEO);
        }
    }

    private void setResult(List<PhotoResultBean> list) {
        StringBuilder builder = new StringBuilder();
        for (PhotoResultBean bean : list) {
            String path = bean.getPath();
            String base64 = bean.getBase64();
            builder.append(path).append("\n");
        }
        tv_Result.setText(builder.toString());
    }


    private void useEmmCamera(int type) {
        Uri uri;
        String endS = type == EMMCameraUtil.IS_PHOTO ? ".png" : ".mp4";
        File file = new File(getFilesDir().getPath(), "test233" + type + endS);
        if (Build.VERSION.SDK_INT >= 24) {//android 7.0以上
            uri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID.concat(".provider"), file);
        } else {
            uri = Uri.fromFile(file);
        }
        EMMCameraUtil.getEmmPhotoOrVideo(this, type, uri);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == EMMCameraUtil.IS_PHOTO && resultCode == RESULT_OK) {
            String path = data.getData().getPath();
            tv_Result.setText("path:" + path);
        } else if (requestCode == EMMCameraUtil.IS_VIDEO && resultCode == RESULT_OK) {
            String path = data.getData().getPath();
            tv_Result.setText("path:" + path);
        }
    }
}
